<template>
  <view class="wrap">
    <u-form class="form" :model="model" :rules="rules" ref="uForm" label-position="left">
      <u-form-item label="岗位编码" prop="postCode" label-width="180">
        <u-input v-model="model.postCode" placeholder="岗位编码"></u-input>
      </u-form-item>
      <u-form-item label="岗位名称" prop="postName" label-width="180">
        <u-input v-model="model.postName" placeholder="岗位名称"></u-input>
      </u-form-item>
      <u-form-item label="岗位排序" prop="postSort" label-width="180">
        <u-input v-model="model.postSort" placeholder="岗位排序"></u-input>
      </u-form-item>
      <u-form-item label="岗位描述" prop="remark" label-width="180">
        <u-input v-model="model.remark" placeholder="岗位描述"></u-input>
      </u-form-item>
    </u-form>
    <view class="form-footer">
      <u-button class="btn" type="primary" @click="submit">提交</u-button>
      <u-button class="btn" type="default" @click="cancel">关闭</u-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      type: '',
      model: {
        postId: 0,
        postCode: '',
        postName: '',
        postSort: '',
        remark: '',
        delFlag: '',
        createTime: '',
        createBy: '',
        updateTime: '',
        updateBy: '',
        tenantId: '',
      },
      rules: {
        postCode: [
          {required: true, message: '岗位编码不能为空', trigger: 'blur'}
        ],

        postName: [
          {required: true, message: '岗位名称不能为空', trigger: 'blur'}
        ],

        postSort: [
          {required: true, message: '岗位排序不能为空', trigger: 'blur'}
        ],

        remark: [
          {required: true, message: '岗位描述不能为空', trigger: 'blur'}
        ],

        delFlag: [
          {required: true, message: '是否删除  -1：已删除  0：正常不能为空', trigger: 'blur'}
        ],

        createTime: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ],

        createBy: [
          {required: true, message: '创建人不能为空', trigger: 'blur'}
        ],

        updateTime: [
          {required: true, message: '更新时间不能为空', trigger: 'blur'}
        ],

        updateBy: [
          {required: true, message: '更新人不能为空', trigger: 'blur'}
        ],

        tenantId: [
          {required: true, message: '租户ID不能为空', trigger: 'blur'}
        ],

      }
    }
  },
  onLoad(params) {
    if (params && params.id !== "undefined") {
      this.type = 'edit'
      this.$u.api.unixPost.getObj(params).then(res => {
        this.model = res.data;
      });
    } else {
      this.type = 'add'
      this.model = {}
    }
  },
  methods: {
    submit() {
      this.$refs.uForm.validate(async valid => {
        if (valid) {
          if (this.type === 'edit') {
            await this.$u.api.unixPost.putObj(this.model)
          } else {
            await this.$u.api.unixPost.addObj(this.model)
          }
          setTimeout(() => {
            uni.showModal({
              title: '提示',
              content: this.type === 'edit' ? '修改成功' : '添加成功',
              showCancel: false,
              success: function () {
                uni.setStorageSync('refreshList', true);
                uni.navigateTo({
                  url: '/pages/sys/unixPost/index'
                })
              }
            })
          }, 200)
        } else {
          this.$u.toast('您填写的信息有误，请根据提示修正。')
        }
      });
    },
    cancel() {
      uni.navigateBack();
    }
  }
}
</script>

